package com.chen.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

// 多线程同步下载图片
public class TestThread2 implements Runnable{

    private String url;
    private String name;

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载的文件名：" + name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("http://www.apache.org/events/current-event-125x125.png", "1.jpg");
        TestThread2 t2 = new TestThread2("https://www.baidu.com/img/flexible/logo/pc/result.png", "2.jpg");
        TestThread2 t3 = new TestThread2("https://portrait.gitee.com/uploads/avatars/user/136/409700_younger11_1579401423.png!avatar60", "3.jpg");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();

    }

}

// 下载器
class WebDownloader{
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");
        }
    }
}
